/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : home.js                                 
*  Description  : 클럽개별메인 
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-06-00  이윤노    Created.  
*  2012-06-00  이윤노    Complete.  
*******************************************************************************/

jQuery(function($){

	$.ajax({
		//url: _ROOT + "/self_club/js/self_club_top.js"
		url: "js/self_club_top.js"
		, type: "get"
		, dataType: "script"
		, async: false
		, success: function(){
			//alert("ok");
		}
		, error: function(){
			//alert("error");
		}
	});
	
	// 동호회 정보
	function clsClubInfo()
	{
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsClubInfo.prototype = {
		_init: function(){
			var $layer = $("div#container ul.self_c_name li:eq(0)");
			
			this._HTMLDom = {
				"$layer": $layer
				, "$banner": $("div#container div.self_club_visual")
			}
			
		}
		, setData: function(thisData){
			this.infoData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $banner = this._HTMLDom["$banner"];
			var thisClass = this;
			
			$layer.contents().filter(function(){
				return (this.nodeType == 3 && this.data.trim())
			}).before(this.infoData["동호회명"]).remove();
			
				
			$layer.find("span:eq(0)").html(this.infoData["개설일"]);
			$layer.find("span.personnel").html(this.infoData["가입자수"]);
			$layer.find("span.comment").html(this.infoData["글수"]);
			if(this.infoData["가입여부"]=="0") {
				//미가입
				$layer.find("span:eq(3) a img").attr("src","../images/btn/btn_join.gif").click(function(event){
					$(thisClass).trigger("clubJoin");
				});
			}else{
				//가입
				$layer.find("span:eq(3) a img").attr("src","../images/btn/btn_out03.gif").click(function(event){
					$(thisClass).trigger("clubClose");					
				});
			}
			
			$banner.find("img").attr("src",this.infoData["배너이미지"]);
			//$banner.find("img").attr("src","http://static.jquery.com/files/rocker/images/logo_jquery_215x53.gif");
		}
		// 가입하기 버튼
		, $getBtnJoin: function(){
			var $layer = this._HTMLDom["$layer"];			
			return $layer.find("span:eq(3) a");
		}
		, setClubInfoData: function(key, val){
			this.infoData[key] = val;	
		}
		, getClubInfoData: function(key){
			return this.infoData[key];	
		}
	}
	
	//나의동호회목록
	function clsClubMyList(){
		this._HTMLDom = null;
		this.clubMyList = null;
		
		this._init();
	}
	clsClubMyList.prototype = {
		_init: function(){
			var $layer = $("div#container ul.self_c_name li.right_note select#select");
			this._HTMLDom = {
				"$layer": $layer	
				, "$element": $layer.find("option:first")
			}
		}
		, setList: function(thisData){
			this.clubMyList = [{
				"동호회명": "나의 동호회 리스트"
				, "동호회키": ""
			}].concat(thisData);
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.empty();
			
			for(var i=0;i<this.clubMyList.length;++i){
				$layer.append($element.clone(true));	
			}
			
			$layer.find("option:first").attr("selected", "selected");
			
			$layer.find("option").each(function(n){			
				$(this).html(thisClass.clubMyList[n]["동호회명"]);
			});
			
			this._bind();
		}
		, _bind: function(){
			var $layer = this._HTMLDom["$layer"];
			var thisClass = this;
			
			$layer.change(function(event){
				var n = $layer.find("option").index($layer.find("option:selected"));				
				document.location.href = "club_view.html?q_my=&club_seq=" + thisClass.clubMyList[n]["동호회키"];					
			});
		}
	}
	
	function clsComm()
	{
		this.clubInfo = null;
		this.clubInfoRequest = null;
		
		this.clubMyList = null;
	
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			this.clubInfoRequest = {
				"동호회키": null
				, "동호회명": null
			}
			
			this.clubData = {
				"동호회명": null
				, "가입자수": null
				, "라운딩키": null
				, "라운딩제목": null
				, "가입여부": null
				, "가입유형": null
			}
		}
//------------------------ 동호회기본정보------------------------//		
		, getClubInfo: function(){
			var thisClass = this;
			var club_seq = this.clubInfoRequest["동호회키"];
			
			//ajax
			setTimeout(function(){
				var rnd = 0;//document.objUtil.random(0, 1);
				var rnd1 = document.objUtil.random(0, 1);
				
				thisClass.clubData = {
					"동호회명": "좋은 만남 훌륭한 배반자 - 헝그리 골프클럽"
					, "동호회키": club_seq
					, "배너이미지": "../images/common/club_visual.jpg"
					, "가입자수": "3,694"
					, "글수": "1,000"
					, "개설일": "2012.05.13(일)"
					, "가입여부": rnd
					, "가입유형": rnd1
					, "가입질문": "이쁘냐?"
					, "내상태": "002"
				}
				
				$(thisClass).trigger("evtFinishClubInfo");
			}, 100);
		}
		, getClubInfoData: function(){
			return this.clubData;
		}
		, setClubInfoRequest: function(key,val){
			this.clubInfoRequest[key] = val;	
		}
		, getClubInfoRequest: function(key){
			return this.clubInfoRequest[key];	
		}
//------------------------ 동호회기본정보------------------------//		
//------------------------ 나의동호회리스트------------------------//		
		, getClubMyList: function(){			
			var thisClass = this;
			
			var szPage = 1;
			var szSortKey = 0;
			var szSortOption = 0;
			
			// ajax 시작			
			//parameter setting and run
			document.objGlobal.ajax({
				"sid":"CL001",
				"servicename":"getMyClubList",
				"listcount":99,
				"currpageno":szPage
			}, {
				  "sortBy": szSortKey
				  , "sortOption": szSortOption
			}, function(ret){
				
				// callback and mapping
				thisClass.clubMyList = [];
			
				if(ret["msp"]["contents"]["items"]){
					
					if(!ret["msp"]["contents"]["items"].length){
						ret["msp"]["contents"]["items"] = new Array(ret["msp"]["contents"]["items"]);
					}
					
					for(var i=0;i<ret["msp"]["contents"]["items"].length;i++){
						var arr = [];
						var obj = ret["msp"]["contents"]["items"][i];
						
						//동호회목록
						thisClass.clubMyList.push({
							"동호회키": obj["clubSeq"]
							, "동호회명": obj["clubName"]
							, "회원수": obj["memberCnt"] 
							, "게시글": obj["newBoardCnt"]
							, "회원정보": {
								"회원구분명": obj["clubMember"]["memberRoleName"]
								, "회원권한": obj["clubMember"]["adminFlag"]
								, "가입일": obj["clubMember"]["joinDate"]
							}
						});
						
					}         
				}
				
				$(thisClass).trigger("evtFinishClubMyList");
			});			
		}
		, getClubMyListData: function(){
			return this.clubMyList;
		}
//------------------------ 나의동호회리스트------------------------//		

	}
	
	function clsControl()
	{
		this.objComm = null;
		
		this.objClubInfo = null
		this.objClubMyList = null;
				
		this._init();
		
		new clsSelfClubTop();
	}
	
	clsControl.prototype = {
		_init: function(){
			var member_id = document.objUtil.getCookie("member_id");
			
			if(!member_id) {
				if(confirm("로그인 후 이용하여 주세요.")){
					document.objGlobal.goLogin();						
				}else{
					document.location.href = "home.html";
				}
				
			}
			
			this.objComm = new clsComm();
			
			this.objClubInfo = new clsClubInfo();			
			this.objClubMyList = new clsClubMyList();			
						
			this._bind();
			
			//동호회정보
			this.objComm.setClubInfoRequest("동호회키",document.objUtil.getParam("club_seq")); 
			this.objComm.getClubInfo();
			
		}
		, _bind: function(){
			var thisClass = this;
						
			// 동호회정보 조회 완료이벤트
			$(this.objComm).bind("evtFinishClubInfo", function(){
				var thisData = thisClass.objComm.getClubInfoData();				
				thisClass.objClubInfo.setData(thisData);
				thisClass.objClubInfo.$getBtnJoin().hide();
				//나의동호회리스트 조회
				thisClass.objComm.getClubMyList();
				
				
				
			});
			
			// 나의동호회 리스트 조회 완료이벤트
			$(this.objComm).bind("evtFinishClubMyList", function(){
				var thisData = thisClass.objComm.getClubMyListData();
				thisClass.objClubMyList.setList(thisData);				
			});
			
			
			// 가입하기
			$(this.objClubInfo).bind("clubJoin", function(event){
				event.preventDefault();				
				if(thisClass.objClubInfo.getClubInfoData("가입유형") == "0"){
					document.location.href = "club_join1.html?club_seq="  + (thisClass.objClubInfo.getClubInfoData("동호회키"));	
				}else if(thisClass.objClubInfo.getClubInfoData("가입유형") == "1"){
					document.location.href = "club_join2.html?club_seq="  + (thisClass.objClubInfo.getClubInfoData("동호회키"));	
				}
			});
			
			//탈퇴하기
			$(this.objClubInfo).bind("clubClose", function(event){
				event.preventDefault();				
				alert("탈퇴하기");
				if(thisClass.objClubInfo.getClubInfoData("가입유형") == "0"){
					document.location.href = "club_join1.html?club_seq="  + (thisClass.objClubInfo.getClubInfoData("동호회키"));	
				}else if(thisClass.objClubInfo.getClubInfoData("가입유형") == "1"){
					document.location.href = "club_join2.html?club_seq="  + (thisClass.objClubInfo.getClubInfoData("동호회키"));	
				}
			});
			
		}
	}
	
	//document.objControl = new clsControl();
});